/*
 * Copyright (c) 2009 Tamás Kovács <keccs *at* inbox.com>
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package keccs.hitTheHay.ui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.KeyStroke;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import keccs.hitTheHay.core.ApplicationModel;
import keccs.hitTheHay.core.ApplicationModel.ApplicationState;

import net.miginfocom.swing.MigLayout;

class TimeChangeListener implements ChangeListener {
	@Override
	public void stateChanged(ChangeEvent event) {
		JSpinner source = (JSpinner) event.getSource();
		int timeSet = (Integer) source.getValue();
		ApplicationModel.getInstance().setTime(timeSet);
	}
}

class TimeMouseWheelListener implements MouseWheelListener {
	@Override
	public void mouseWheelMoved(MouseWheelEvent event) {
		JSpinner spinner = (JSpinner) event.getSource();
		String equivalentKeyStrokeName = (event.getWheelRotation() < 0 ? "UP" : "DOWN"); //$NON-NLS-1$ //$NON-NLS-2$
		ActionListener actionForKeyStroke = spinner.getActionForKeyStroke(KeyStroke.getKeyStroke(equivalentKeyStrokeName));
		ActionEvent actionEvent = new ActionEvent(event.getSource(), event.getID(), null);
		actionForKeyStroke.actionPerformed(actionEvent);
	}
}

final class TimePanel extends JPanel {
	private static final long serialVersionUID = 1L;

	private class ApplicationStateChangeListener implements PropertyChangeListener {
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			timeSpinner.setEnabled(evt.getNewValue() == ApplicationState.INPUT);
		}
	}

	private static final Font LARGE_FONT = UIManager.getLookAndFeelDefaults().getFont("Panel.font").deriveFont(20f); //$NON-NLS-1$

	JSpinner timeSpinner;

	public TimePanel() {
		super();
		setBorder(new TitledBorder(Messages.getString("TimePanel.WhenToDoIt"))); //$NON-NLS-1$
		setLayout(new MigLayout("fill", "[fill|grow 0]")); //$NON-NLS-1$ //$NON-NLS-2$

		int defaultTime = ApplicationModel.getInstance().getTime();
		SpinnerNumberModel spinnerModel = new SpinnerNumberModel(defaultTime, 1, 1000, 5);
		timeSpinner = new JSpinner(spinnerModel);
		timeSpinner.setFont(LARGE_FONT);
		timeSpinner.addChangeListener(new TimeChangeListener());
		timeSpinner.addMouseWheelListener(new TimeMouseWheelListener());
		add(timeSpinner);

		add(new JLabel(Messages.getString("TimePanel.MinutesFromNow")), "wrap, aligny center"); //$NON-NLS-1$ //$NON-NLS-2$

		ApplicationModel.getInstance().addPropertyChangeListener("state", new ApplicationStateChangeListener()); //$NON-NLS-1$
	}
}